<?php
namespace app\user\model\user\admin;

use app\common\model\BaseModel;
use app\common\model\DeviceType;
use app\user\model\user\Account;

/**
 * 用户登录记录
 */
class UserLoginRecord extends BaseModel
{
    /**
     * 表名
     * @var string
     */
    protected $name = 'user_login_record';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 不写入更新时间
     * @var boolean
     */
    protected $updateTime = false;

    /**
     * 定义表主键
     * @var string
     */
    protected $pk = 'id';
    /**
     * 填充字段
     * @var [type]
     */
    protected $append = ['device_text'];

    /**
     * 设备选项数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-04
     * @return   [type]                         [description]
     */
    public function getDeviceTypeOptions()
    {
        $options = [];

        $devices = DeviceType::getDeviceList();
        foreach ($devices as $key => $value) {
            $options[] = ['title' => $value, 'value' => $key];
        }

        return $options;
    }

    /**
     * 关联用户
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-30
     * @return   [type]                         [description]
     */
    public function user()
    {
        return $this->belongsTo(Account::class, 'user_id')->with(['avatar_url']);
    }

    /**
     * 登录设备
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-04
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getDeviceTextAttr($value, $data)
    {
        $text = DeviceType::getDeviceList();

        return $text[$data['device']] ?? '未知';
    }

}
